package run.jingjing.websocket.model;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: jingjing
 * @date: 2021/6/24
 * @description:
 */
@Slf4j
public class LWsTopic extends ConcurrentHashMap<Integer, LWsSession> {

    private LWsGroup lWsGroup;

    private String name;

    public LWsGroup getlWsGroup() {
        return lWsGroup;
    }

    public void setlWsGroup(LWsGroup lWsGroup) {
        this.lWsGroup = lWsGroup;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public LWsSession put(Integer userId, LWsSession lWsSession) {
        LWsSession origin = super.get(userId);
        if (origin != null) {
            try {
                origin.getSession().close();
            } catch (IOException e) {
                log.error("close repetitive connection occurred error");
                throw new RuntimeException();
            }
            log.info("userId:{} old connection existed in local, it will be closed by new connection", userId);
        }
        return super.put(userId, lWsSession);
    }
}
